﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HandyControl.Controls;
using Model;
 
using RestSharp;
using RestSharp.Authenticators;
namespace UI.View.Base.Rooms
{
    /// <summary>
    /// List.xaml 的交互逻辑
    /// </summary>
    public partial class RoomList : Page
    {
        RestClient client = new RestClient(new Uri(Comm.TempData.BaseUrl));

        ViewModel.BuildViewModel vm;
        public RoomList()
        {
            InitializeComponent();

            vm = DataContext as ViewModel.BuildViewModel;

            vm.CurrRoom= new Model.Rooms();
            LoadData();

            


        }

        //PmsApi api = new PmsApi();

        private async  void LoadData()
        {
            try
            {
                var request = new RestRequest("room", RestSharp.DataFormat.Json);


                var response = await client.GetAsync<ListDataResponse<Model.Rooms>>(request);
                vm.RoomList = response.data;


                request = new RestRequest("community", Method.GET);

                var commRep = await client.GetAsync<ListDataResponse<Model.Community>>(request);

                vm.Communities = commRep.data;


                request = new RestRequest("building", Method.GET);

                var builds = await client.GetAsync<ListDataResponse<Model.Building>>(request);

                vm.BuildingList = builds.data;


                cmb_Build.ItemsSource = vm.BuildingList;
                cmb_Build.DisplayMemberPath = "Name";
                cmb_Build.SelectedValuePath = "Id";

            }
            catch (Exception ex)
            {

                HandyControl.Controls.Growl.FatalGlobal(ex.Message);
            }
            
             
        }


  
        private void btn_Create_Click(object sender, RoutedEventArgs e)
        {
            sp_edit.IsEnabled = true;
            vm.CurrRoom= new  Model.Rooms();
            vm.IsEditRoom = false;
        }

        private void btn_Cancle_Click(object sender, RoutedEventArgs e)
        {
            sp_edit.IsEnabled = false;
        }

        private async void btn_save_click(object sender, RoutedEventArgs e)
        {
            if (cmb_build.SelectedItem == null)
            {
                Growl.WarningGlobal("请选择楼栋信息！");
                return;
            }
            if (string.IsNullOrEmpty(vm.CurrRoom.No))
            {
                Growl.WarningGlobal("房屋编号不能为空！");
                return;
            }

          

            if (vm.IsEditRoom)
            {
                if (vm.CurrRoom != null)
                {
                    var res = new RestRequest("room", Method.PUT);



                    vm.CurrRoom.BuildingId = vm.CurrBuild.Id;
                    vm.CurrRoom.BuilingNo = vm.CurrBuild.No;
                    res.AddJsonBody(vm.CurrRoom);
                    
                   var response =  await client.ExecuteAsync<SingleDataResponse<long>>(res);


                    if(response.Data.succeeded)
                    {
                         Growl.SuccessGlobal("修改成功！");
                    }
                    else
                    {

                        Growl.SuccessGlobal("修改失败！");
                        
                    }
                }
            }
            else
            {
                if (vm.CurrRoom != null)
                {

                    



                    var res = new RestRequest("room", Method.POST);
                    res.AddJsonBody(vm.CurrRoom);
                  
                    var response = await client.ExecuteAsync<SingleDataResponse<int>>(res);

                    if (response.IsSuccessful)
                    {
                        Growl.SuccessGlobal("保存成功！");
                    }
                    else
                    {
                        Growl.SuccessGlobal(response.ErrorMessage);
                       // Growl.SuccessGlobal(response.Data.errors.messages[0]);
                        Growl.SuccessGlobal("保存失败！");
                    }
                }
            }
            

            LoadData();
        }

        private async void btn_Delete_Clicl(object sender, RoutedEventArgs e)
        {
            
            var res = new RestRequest("room/" + vm.CurrRoom.Id, Method.DELETE);
            
            var api = new PmsApi();
           var ret = await api.Delete<bool>(res);
            if(ret)
            {
                HandyControl.Controls.Growl.SuccessGlobal("删除成功！");
            }
            else
            {
                HandyControl.Controls.Growl.ErrorGlobal("删除失败！");
            }

            LoadData();
        }

        private void btn_Edit_Click(object sender, RoutedEventArgs e)
        {
            sp_edit.IsEnabled = true;
            
            vm.IsEditBuild = true;
        }

        Model.DataContext context = new DataContext(); 
        private async void Btn_Query_Click(object sender, RoutedEventArgs e)
        {
            var Bid = 0; ;
            var No = txt_roomNo.Text;
            var name = txt_OwnerName.Text;
            var query = context.DB.Queryable<Model.Rooms>();
            if ( cmb_Build.SelectedValue !=null)
            {
                Bid = (int)cmb_Build.SelectedValue;
                query = query.Where(r => r.BuildingId  == Bid);
            }

            if (!string.IsNullOrEmpty(No))
            {
                query = query.Where(b => b.No.Contains( No));
            }
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(b => b.OwnerName.Contains( Name));
            }



            vm.RoomList = await query.ToListAsync();
        }

         

        private void txt_RoomNo_GotFocus(object sender, RoutedEventArgs e)
        {
            if(cmb_build.SelectedItem !=null)
            {
                vm.CurrBuild = cmb_build.SelectedItem as Model.Building;
                txt_RoomNo.Text = $"{vm.CurrBuild.Id}-{txt_Unit.Value}-";
            }
        }
    }
}
